<?php
/**
 * Created by PhpStorm.
 * User: Dinh Thi
 * Date: 2/8/15
 * Time: 10:22 PM
 */

namespace MyApp\Application\Controller;


use MyApp\Application\Model\KhachHangModel;
use DinhThi\MVC\Request;

class KhachHangController extends MyAppAbstractController{
    public function __construct(){
        parent::__construct();
        parent::mustBeLogin();

    }

    public function indexAction(){
        $this->view->render('KhachHang/index');
    }

    public function QuanLyAction(){
        $this->view->addHeaderScript('Public/js/KhachHang/QuanLy.js');

        $this->view->setTitle('Quản lý khách hàng');
        $this->view->render('KhachHang/quanly');
    }

    public function xemthongtinAction(){

    }


    /*=================Ajax================*/
    public function LuuThongTinAction(){
        if (isset($_POST['id'])){
            $id             = $_POST['id'];
            $hoten          = $_POST['hoten'];
            $sodienthoai    = $_POST['sodienthoai'];
            $diachi         = $_POST['diachi'];

            $model = new KhachHangModel();
            if ($id == -1){
                echo json_encode($model->TaoKhachHang($hoten, $diachi, $sodienthoai));
            } else{
                echo json_encode($model->SuaKhachHang($id, $hoten, $diachi, $sodienthoai));
            }
        }
    }

    public function DanhSachKhachHangAction(){
        $model = new KhachHangModel();
        echo json_encode($model->DanhSachKhachHang());
    }

    public function ThongTinGiaoDichAction(){
        $request = Request::getInstance();
        if (isset ($request->getParams()[2])){
            $model = new KhachHangModel();
            echo json_encode($model->ThongTinGiaoDich($request->getParams()[2]));
        }
    }

    public function ChiTietGiaoDichAction(){
        $request = Request::getInstance();
        if (isset ($request->getParams()[2])){
            $model = new KhachHangModel();
            echo json_encode($model->ChiTietGiaoDich($request->getParams()[2]));
        }
    }



    public function QuyetToanCongNoAction(){
        if (isset ($_POST['id'])){
            $id = $_POST['id'];
            $sotien = $_POST['sotien'];
            $ghichu = $_POST['ghichu'];
            $model = new KhachHangModel();
            $model->QuyetToanCongNo($id, $sotien, $ghichu);
        }
    }
} 